package com.aat.dizena.base.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aat.dizena.base.bean.Ress;
import com.aat.dizena.base.service.RessService;
import com.aat.dxfy.utils.Atools;

/**
 * @time 2016-05-25
 * @author 新歌<xingeking@163.com>
 * @email xingeking@163.com
 * @version v1.0
 * @function 资源菜单管理
 * @info java spring aop
 * @update none
 */
@Controller
@Scope("prototype")
@RequestMapping("master/ress")
public class MasterRessController extends BaseController {

	@Autowired
	private RessService ressService;

	private String list = "/master/ress/list";
	private String edit = "/master/ress/edit";
	private String reList = "redirect:" + list;
	private String addAction = "master/ress/add";
	private String uptAction = "master/ress/upt";
	private String delAction = "master/ress/del";
	private String StringKey = "资源";
	private String addTitle = "增加" + StringKey;
	private String uptTitle = "修改" + StringKey;
	private String listHead = StringKey + "管理";

	// list get
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model m) {
		m.addAttribute("addAction", addAction);
		m.addAttribute("uptAction", uptAction);
		m.addAttribute("delAction", delAction);
		m.addAttribute("listHead", listHead);
		m.addAttribute("list", list);
		return list;
	}

	// list post
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(@RequestParam(value = "s", required = false, defaultValue = "1") int start,
			@RequestParam(value = "p", required = false, defaultValue = "10") int perPage,
			@RequestParam(value = "f", required = false, defaultValue = "resSort") String sortFiled,
			@RequestParam(value = "dir", required = false, defaultValue = "ASC") String direction,

	@RequestParam(value = "type", required = false) String resType,
			@RequestParam(value = "name", required = false) String resName,
			@RequestParam(value = "level", required = false) Integer resLevel,
			@RequestParam(value = "pid", required = false) String resPid,
			@RequestParam(value = "u", required = false) String skey) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sortFiled", sortFiled);
		params.put("direction", direction);
		params.put("start", (start - 1) * perPage);
		params.put("perpage", perPage);
		// 其他参数
		if (isNotEmpty(skey)) {
			params.put("skey", skey);
		}
		// 其他参数
		if (isNotEmpty(resType)) {
			params.put("resType", resType);
		}
		if (isNotEmpty(resName)) {
			params.put("resName", resName);
		}
		if (null != resLevel) {
			params.put("resLevel", resLevel);
		}
		if (isNotEmpty(resPid)) {
			params.put("resPid", resPid);
		}
		// 返回参数
		List<Ress> lists = ressService.queryAll(params);
		Long count = ressService.queryCount(params);
		Long tmp = count / perPage;
		Long pages = count % perPage == 0 ? tmp : tmp + 1;
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("datas", lists);// 数据
		json.put("pages", pages);// 总页
		json.put("start", start);// 页码
		json.put("count", count);// 总数
		return json;
	}

	// add get
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("action", addAction);
		model.addAttribute("title", addTitle);
		Ress m = new Ress();
		m.setId(Atools.getOneKeyS());
		m.setResAuth("none");
		m.setResIcon("fa fa-star");
		m.setResType("1");
		m.setResPid("0");
		model.addAttribute("m", m);
		return edit;
	}

	// add post
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Ress m, Model model) {
		model.addAttribute("action", addAction);
		model.addAttribute("title", addTitle);
		model.addAttribute("m", m);
		// 验证
		if (isEmpty(m.getResName())) {
			model.addAttribute("error", "名称不为空");
			return edit;
		}
		if (isEmpty(m.getResUrl())) {
			model.addAttribute("error", "链接不为空");
			return edit;
		}

		if (!"1".equals(m.getResType())) {
			m.setResLevel(1);
			m.setResPid("0");
		}

		m.setLocked("0");
		// end

		ressService.addBean(m);
		return reList;
	}

	// upt get
	@RequestMapping(value = "upt/{id}", method = RequestMethod.GET)
	public String upt(@PathVariable("id") String id, Model model) {
		model.addAttribute("action", uptAction);
		model.addAttribute("title", uptTitle);
		Ress m = ressService.queryBean(id);
		model.addAttribute("m", m);
		return edit;
	}

	// upt post
	@RequestMapping(value = "upt", method = RequestMethod.POST)
	public String upt(Ress m, Model model) {
		model.addAttribute("action", uptAction);
		model.addAttribute("title", uptTitle);
		model.addAttribute("m", m);
		// 验证
		if (isEmpty(m.getResName())) {
			model.addAttribute("error", "名称不为空");
			return edit;
		}
		if (isEmpty(m.getResUrl())) {
			model.addAttribute("error", "链接不为空");
			return edit;
		}

		if (!"1".equals(m.getResType())) {
			m.setResLevel(1);
			m.setResPid("0");
		}

		m.setLocked("0");
		// end
		ressService.updateBean(m);
		return reList;
	}

	// del
	@RequestMapping(value = "del/{id}", method = RequestMethod.GET)
	public String del(@PathVariable("id") String id) {
		ressService.deleteBean(id);
		return reList;
	}

	// 取得菜单json
	@RequestMapping("getMTree")
	@ResponseBody
	public List<Map<String, Object>> getMenuTreeData() {
		List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("id", "0");
		json.put("text", "所有菜单");
		json.put("nodes", getMenuTreeData("0", 1));
		lists.add(json);
		return lists;
	}

	private List<Map<String, Object>> getMenuTreeData(String pid, Integer level) {
		List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
		List<Ress> ress = ressService.queryResByLP(pid, level);
		for (Ress r : ress) {
			if (r != null) {
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("id", r.getId());
				m.put("text", r.getResName());
				List<Map<String, Object>> tmpLists = getMenuTreeData(r.getId(), r.getResLevel() + 1);
				if (tmpLists.size() > 0) {
					m.put("nodes", tmpLists);
				}

				lists.add(m);
			}
		}
		return lists;
	}

	// 取得排序的数据
	@RequestMapping("getResSort")
	@ResponseBody
	public Integer getResSort(@RequestParam("t") String t, @RequestParam("l") Integer l, @RequestParam("p") String pid) {
		Ress tmp = ressService.queryBean(pid);
		Integer i = ressService.queryResSort(t, l, pid);
		if(tmp == null && i== null){
			i=1;
		}else if(tmp != null && i== null){
			 i = ressService.queryBean(pid).getResSort();
			 i=i*100+1;
		}else  if (tmp != null && i!=null&&i == 1) {
			i = tmp.getResSort() * 100 + i;
		}else{
			i++;
		}
		return i;
	}

	// 取得父级菜单
	@RequestMapping("queryParentRes")
	@ResponseBody
	public List<Ress> queryParentRes(@RequestParam("t") String t, @RequestParam("l") String l) {
		List<Ress> ress = new ArrayList<Ress>();
		if ("1".equals(l)) {
			Ress r = new Ress();
			r.setId("0");
			r.setResName("根目录");
			ress.add(r);
		} else if ("2".equals(l)) {
			ress = ressService.queryParentRes(t, 1);
		} else if ("3".equals(l)) {
			ress = ressService.queryParentRes(t, 2);
		}
		return ress;
	}

	//
}
